<template>
  <el-card class="m-8">


    <section slot="header" class="mb-10 flex items-center">
      <el-button class="mr-4" type="primary" @click="addIndepUser">新增奖励</el-button>
    </section>

    <el-descriptions :column="3" border class="my-5">
      <template slot="extra">
        <el-button type="primary" @click="saveInfoConfig">保存</el-button>
      </template>
      <el-descriptions-item>
        <template slot="label">
          活动名称
        </template>
        建造达人
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          活动状态
        </template>
        <el-tag v-if="form.is_open===1" size="small">开启</el-tag>
        <el-tag v-else size="small">关闭</el-tag>
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          奖励状态
        </template>
        <el-tag v-if="form.prize_status===1" size="small">启用</el-tag>
        <el-tag v-else size="small">停用</el-tag>
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          系统通知标题
        </template>
        <el-input v-model="form.notice_title" style="width: 100%"></el-input>
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          系统通知话术
        </template>
        <el-input v-model="form.notice_text" style="width: 100%"></el-input>
      </el-descriptions-item>
    </el-descriptions>


    <section v-if="tableList.length">
      <h1 class="text-2xl">奖励配置</h1>
      <el-popconfirm
        cancel-button-text="不用了"
        confirm-button-text="好的"
        icon="el-icon-info"
        icon-color="red"
        title="确定删除吗？"
        @confirm="delConfig"
      >
        <el-button slot="reference" type="danger">删除</el-button>
      </el-popconfirm>

      <el-table :data="tableList" :span-method="objectSpanMethod" align="center" border
                class="mt-4" stripe
      >
        <el-table-column label="榜单对象" prop="ladder"/>
        <el-table-column label="榜单类型" prop="dayLadder"/>
        <el-table-column label="榜单排名" prop="rank"/>
        <el-table-column label="奖励图标">
          <template slot-scope="scope">
            <img
              v-if="scope.row.picture"
              :src="scope.row.picture"
              alt=""
              style="max-width: 200px; max-height: 200px"
            />
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column label="奖励名称">
          <template slot-scope="scope">
            <span>能量</span>
          </template>
        </el-table-column>
        <el-table-column label="奖励数量" prop="percent"/>
        <el-table-column label="奖励状态" prop="status">
          <template slot-scope="{row}">
            <span v-if="row.status===1">启用</span>
            <span v-else>禁用</span>
          </template>
        </el-table-column>
        <el-table-column label="操作人" prop="admin.admin_name"/>
        <el-table-column label="操作时间" prop="created_at"/>
        <el-table-column align="center" label="操作">
          <template slot-scope="{row}">
            <el-button plain @click="addIndepUser(row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--      <cus-pagination-->
      <!--        :limit.sync="dataForm.per_page"-->
      <!--        :page.sync="dataForm.page"-->
      <!--        :total="total"-->
      <!--        @pagination="changeGame"-->
      <!--      />-->

    </section>


    <el-dialog v-dialogDrag :title="!isEdit?'新增':'编辑'" :visible.sync="dialogFormVisible"
               @close="handleCloseDialog('addForm')"
    >
      <el-form ref="addForm" :model="dynamicValidateForm" :rules="rules">
        <el-form-item label="榜单对象">
          <el-input :disabled="true" value="欧气榜"/>
        </el-form-item>

        <el-form-item label="榜单类型">
          <el-input :disabled="true" value="日榜"/>
        </el-form-item>

        <el-form-item label="榜单对象">
          <el-input :disabled="true" value="欧气榜"/>
        </el-form-item>

        <el-form-item
          v-if="!isEdit"
          label="设置名次"
          prop="total"
        >
          <el-input v-model="dynamicValidateForm.total"></el-input>
        </el-form-item>

        <el-form-item
          v-if="isEdit"
          label="数量"
          prop="percent"
        >
          <el-input v-model="dynamicValidateForm.percent"></el-input>
        </el-form-item>

        <el-form-item
          v-if="isEdit"
          label="状态"
          prop="is_open"
        >
          <el-radio-group v-model="dynamicValidateForm.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="handleAddUser('addForm')">保存</el-button>
          <el-button type="danger" @click="dialogFormVisible = false">取消</el-button>
        </span>
    </el-dialog>


  </el-card>
</template>

<script>
import {
  addRewardConfig,
  delRewardConfig,
  EditRewardConfig,
  giftList,
  setConfig
} from '../../api/guardActivity/rewardSend'

export default {
  data() {
    return {
      form: {
        is_open: '',
        prize_status: '',
        notice_title: '',
        notice_text: ''
      },
      ladderOptions: [
        { val: 0, name: '欧气榜' }
      ],
      ladderStatusOptions: [
        { val: 0, name: '日榜' },
        { val: 1, name: '周榜' },
        { val: 2, name: '总榜' }
      ],
      sendOptions: [
        { val: 0, name: '能量' }
      ],
      rowId: '',
      tempOptions: [],
      options: [],
      gameId: '',
      hasGame: false,
      isEdit: false,
      dynamicPoolForm: {
        status_id: 0,
        template_id: 0,
        profit_loss_rate: 1,
        send_id: 0
      },
      dialogPoolVisible: false,
      detailFormVisible: false,
      setStatus: 0,
      dialogFormVisible: false,
      dynamicValidateForm: {
        total: 0,
        percent: 0,
        status: ''
      },
      rules: {
        total: [{ required: true, message: '请输入', trigger: 'blur' }],
        percent: [{ required: true, message: '请输入', trigger: 'blur' }],
        status: [{ required: true, message: '请选择状态', trigger: 'change' }]
      },
      times: [],
      prizeData: [],
      inputVal: 0,
      outputVal: 0,
      radio: 1,
      min: '',
      max: '',
      statusOptions: [
        { val: 0, name: '禁用' },
        { val: 1, name: '启用' }
      ],

      setDialogVisible: false,
      tableList: [],
      dateList: undefined,
      dataForm: {
        app_id: '',
        is_open: '',
        user_code: '',
        nickname: '',
        page: 1,
        per_page: 10
      },
      meta: {
        page: 1,
        perPage: 10
      },
      total: 1,
      id: '',
      // 拒绝原因
      setting: {
        balance_coin: 0
      },
      info: ''
    }
  },
  created() {
    this.initData()
  },
  methods: {
    async saveInfoConfig() {

      setConfig(this.form).then((response) => {
        if (response.code === 200) {
          this.initData()
          this.$notify({
            type: 'success',
            message: '操作成功',
            position: 'bottom-right',
            showClose: false,
            duration: 3000
          })
        }
      })
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if ([0, 1].includes(columnIndex)) {
        if (rowIndex === 0) {
          return {
            rowspan: this.tableList.length,
            colspan: 1
          }
        } else {
          return {
            rowspan: 0,
            colspan: 0
          }
        }
      }
    },
    handleCloseDialog(formName) {
      if (this.$refs[formName]) {
        this.$refs[formName].clearValidate() // 清除整个表单的校验
      }
    },
    handleSet(row) {
      //todo 设置当前行内容的默认状态
      this.setDialogVisible = true
    },
    async delConfig() {
      delRewardConfig().then((response) => {
        if (response.code === 200) {
          this.initData()
          this.$notify({
            type: 'success',
            message: '操作成功',
            position: 'bottom-right',
            showClose: false,
            duration: 3000
          })
        }
      })
    },
    handleAddUser(val) {

      this.$refs[val].validate(valid => {
        if (valid) {
          let obj = {}
          for (let key in this.dynamicValidateForm) {
            if (this.dynamicValidateForm[key] !== undefined && this.dynamicValidateForm[key] !== '') {
              obj[key] = this.dynamicValidateForm[key]
            }
          }

          if (!this.isEdit) {
            addRewardConfig(this.dynamicValidateForm.total).then(res => {
              if (res.code === 200) {
                /**todo 输入几个数量塞几个*/
                for (let i = 0; i <= this.dynamicValidateForm.total; i++) {
                  this.tableList.push({
                    ladder: '欧气榜',
                    dayLadder: '日榜',
                    rank: i + 1,
                    percent: 0,
                    status: 2
                  })
                }
                this.initData()
                this.dialogFormVisible = false
                this.$notify({
                  type: 'success',
                  message: '操作成功',
                  position: 'bottom-right',
                  showClose: false,
                  duration: 3000
                })
              }
            })
          } else {
            obj.id = this.rowId
            EditRewardConfig(obj).then(res => {
              if (res.code === 200) {
                this.initData()
                this.dialogFormVisible = false
                this.$notify({
                  type: 'success',
                  message: '操作成功',
                  position: 'bottom-right',
                  showClose: false,
                  duration: 3000
                })
              }
            })
          }

        } else {
          return false
        }
      })
    },
    addIndepUser(row) {
      if (row.id) {
        this.rowId = row.id
        this.isEdit = true
        this.dynamicValidateForm = {
          total: 0,
          percent: row.percent,
          status: row.status
        }
      } else {
        this.dynamicValidateForm = {
          total: 0,
          percent: 0,
          status: ''
        }
        this.rowId = ''
        this.isEdit = false
      }
      this.dialogFormVisible = true
    },
    async initData() {
      try {
        const { data } = await giftList()
        this.form = {
          is_open: data.is_open,
          prize_status: data.prize_status,
          notice_title: data.notice_title,
          notice_text: data.notice_text
        }
        this.tableList = data.rank_list.map((item) => {
          item.ladder = '欧气榜'
          item.dayLadder = '日榜'
          return item
        })
      } catch (e) {
        console.error(e)
      }
    }

  }
}
</script>

<style scoped>
.el-input {
  width: 200px;
}

/deep/ .el-table .cell {
  flex-wrap: wrap;
}
</style>
